package com.ittx.dzt.tiktok.handler;

import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.business.constant.RocketmqTopicConstant;
import com.ittx.dzt.edi.dto.EdiBatchSplitDto;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;

import java.util.Map;

/***
 * 根据时间下载的接口需要抽象类,支持三种模式
 */
@Slf4j
public abstract class AbstractTiktokByTimeApiHandler<T> extends AbstractBaseTiktokApiHandler<T> {

    @Override
    public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, T t) {
        Result<?> result;
        //模式分发
        String queryType = StrUtil.isEmpty(platformApiParamDTO.getQueryType()) ? open.constant.PlatformApiConstant.ApiByTime.NORMAL : platformApiParamDTO.getQueryType();
        switch (queryType) {
            case PlatformApiConstant.ApiByTime.ID:
                result = this.doProcessByIdType(platformApiParamDTO, t);
                break;
            case PlatformApiConstant.ApiByTime.CHECK:
                result = this.doProcessByCheckType(platformApiParamDTO, t);
                break;
            case PlatformApiConstant.ApiByTime.NORMAL:
                result = this.doProcessByNormalType(platformApiParamDTO, t);
                break;
            default:
                result = Result.error("模式分发异常,请检查请求模式参数[{}]", platformApiParamDTO.getQueryType());
        }
        return result;
    }

    /**
     * 发送数据请求参数和数据id(销售单号/售后单号/商品编码) 到MQ,异步执行单条数据的下载
     *
     * @param platformApiParamDTO
     * @param platformIdentifierTag
     * @param logMap
     */
    protected void sendToSplitTopic(PlatformApiParamDTO platformApiParamDTO, String platformIdentifierTag, Map<String, Boolean> logMap) {
        EdiBatchSplitDto ediBatchSplitDto = new EdiBatchSplitDto(platformApiParamDTO, platformIdentifierTag);
        SendResult sendResult = rocketmqProducer.sendSyncMsg(this.getMqTopic(), RocketmqTopicConstant.DataCenter.DATA_DEFAULT_TAG, ediBatchSplitDto, ediBatchSplitDto.getPlatformIdentifierTag());
        if (sendResult.getSendStatus() != SendStatus.SEND_OK) {
            //分发到MQ失败
            logMap.put(ediBatchSplitDto.getPlatformIdentifierTag(), false);
        } else {
            logMap.put(ediBatchSplitDto.getPlatformIdentifierTag(), true);
        }
    }

    /**
     * 数据String 转为Map
     *
     * @param rdsResponse
     * @return
     */
    public Map<String, Object> responseToMap(Object rdsResponse) {
        if (rdsResponse instanceof Map) {
            return (Map<String, Object>) rdsResponse;
        } else if (rdsResponse instanceof String) {
            return JacksonTool.toMap(rdsResponse);
        }
        log.error("暂不支持转换的原报文数据类型");
        return null;
    }

    /**
     * 异步发送数据请求参数和数据id(销售单号/售后单号/商品编码) 到MQ,异步执行单条数据的下载
     *
     * @param platformApiParamDTO
     * @param platformIdentifierTag
     */
    protected void sendToSplitTopic(PlatformApiParamDTO platformApiParamDTO, String platformIdentifierTag) {
        EdiBatchSplitDto ediBatchSplitDto = new EdiBatchSplitDto(platformApiParamDTO, platformIdentifierTag);
        rocketmqProducer.sendAsyncMsg(this.getMqTopic(), RocketmqTopicConstant.DataCenter.DATA_DEFAULT_TAG, ediBatchSplitDto, ediBatchSplitDto.getPlatformIdentifierTag());
    }


    /**
     * 直接发送数据到数据中心---不经过拆分队列
     */
    protected void sendOut(PlatformApiParamDTO platformApiParamDTO, String platformIdentifierTag, Object data, Map<String, Boolean> logMap) {
        // 每条数据携带上单号
        platformApiParamDTO.setPlatformIdentifierTag(platformIdentifierTag);
        // 发送到下游
        Map<String, Object> dataMap = responseToMap(data);
        Result<?> tempResult = this.sendRequestToPlatform(platformApiParamDTO, dataMap, true);
        if (tempResult.isSuccess()) {
            logMap.put(platformIdentifierTag, true);
        } else {
            logMap.put(platformIdentifierTag, false);
        }
    }


    /**
     * 普通模式执行
     *
     * @param platformApiParamDTO
     * @param t
     * @return
     */
    protected abstract Result doProcessByNormalType(PlatformApiParamDTO platformApiParamDTO, T t);

    /**
     * 单量探查模式执行
     *
     * @param platformApiParamDTO
     * @param t
     * @return
     */
    protected abstract Result doProcessByCheckType(PlatformApiParamDTO platformApiParamDTO, T t);

    /**
     * 单条数据模式执行
     *
     * @param platformApiParamDTO
     * @param t
     * @return
     */
    protected abstract Result doProcessByIdType(PlatformApiParamDTO platformApiParamDTO, T t);

    /**
     * 方法基准mq-topic
     */
    protected abstract String getMqTopic();


}
